package com.example.seabedmusic.views;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.seabedmusic.R;
import com.example.seabedmusic.adapter.SettingsAdapter;
import com.example.seabedmusic.entity.SettingItem;
import com.example.seabedmusic.utils.UserManager;
import java.util.ArrayList;
import java.util.List;

public class SettingsActivity extends AppCompatActivity implements SettingsAdapter.OnSettingItemClickListener {

    private RecyclerView rvSettings;
    private SettingsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        initViews();
        setupSettingsList();
    }

    private void initViews() {
        rvSettings = findViewById(R.id.rvSettings);
        rvSettings.setLayoutManager(new LinearLayoutManager(this));
    }

    // 在 SettingsActivity.java 的 setupSettingsList() 方法中，修改图标引用：
    private void setupSettingsList() {
        List<SettingItem> settingItems = new ArrayList<>();

        // 添加设置项 - 使用正确的图标名称
        settingItems.add(new SettingItem("账号与安全", R.drawable.ic_account_security, SettingItem.TYPE_NORMAL));
        settingItems.add(new SettingItem("通用", R.drawable.ic_settings_general, SettingItem.TYPE_NORMAL));
        settingItems.add(new SettingItem("通知", R.drawable.ic_notifications, SettingItem.TYPE_NORMAL));
        settingItems.add(new SettingItem("隐私与权限", R.drawable.ic_privacy, SettingItem.TYPE_NORMAL));
        settingItems.add(new SettingItem("未成年模式", R.drawable.ic_child_care, SettingItem.TYPE_NORMAL));
        settingItems.add(new SettingItem("关于", R.drawable.ic_info, SettingItem.TYPE_NORMAL));
        settingItems.add(new SettingItem("切换账号", R.drawable.ic_switch_account, SettingItem.TYPE_NORMAL));
        settingItems.add(new SettingItem("退出登录", R.drawable.ic_logout, SettingItem.TYPE_LOGOUT));
        settingItems.add(new SettingItem("关闭", R.drawable.ic_close, SettingItem.TYPE_CLOSE));

        adapter = new SettingsAdapter(settingItems);
        adapter.setOnSettingItemClickListener(this);
        rvSettings.setAdapter(adapter);
    }

    // 在 SettingsActivity.java 的 onSettingItemClick 方法中修改：
    @Override
    public void onSettingItemClick(SettingItem item) {
        switch (item.getTitle()) {
            case "账号与安全":
                Intent accountIntent = new Intent(this, AccountSecurityActivity.class);
                startActivity(accountIntent);
                break;
            case "通用":
                Intent generalIntent = new Intent(this, GeneralSettingsActivity.class);
                startActivity(generalIntent);
                break;
            case "通知":
                Intent notificationIntent = new Intent(this, NotificationSettingsActivity.class);
                startActivity(notificationIntent);
                break;
            case "隐私与权限":
                Intent privacyIntent = new Intent(this, PrivacySettingsActivity.class);
                startActivity(privacyIntent);
                break;
            case "未成年模式":
                Intent teenModeIntent = new Intent(this, TeenModeActivity.class);
                startActivity(teenModeIntent);
                break;
            case "关于":
                Intent aboutIntent = new Intent(this, AboutActivity.class);
                startActivity(aboutIntent);
                break;
            case "切换账号":
                switchAccount();
                break;
            case "退出登录":
                logout();
                break;
            case "关闭":
                finish();
                break;
        }
    }

    private void switchAccount() {
        new AlertDialog.Builder(this)
                .setTitle("切换账号")
                .setMessage("确定要切换账号吗？")
                .setPositiveButton("确定", (dialog, which) -> {
                    UserManager.getInstance(this).logout();
                    navigateToLogin();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void logout() {
        new AlertDialog.Builder(this)
                .setTitle("退出登录")
                .setMessage("确定要退出登录吗？")
                .setPositiveButton("确定", (dialog, which) -> {
                    UserManager.getInstance(this).logout();
                    navigateToLogin();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    private void navigateToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }
}